import mongoose from "mongoose";
import utils from "../utils";
import { isEmail, isPhone } from "../config";

export interface IGood extends mongoose.Document {
  type: any[];
  startEndCity: any;
  aimPlace: any;
  meetPlace: any;
  spendTime: string;
  imgs: any;
  durationTeam: string;
  phone: string;
  clubName: string;
  enrollTpl: any[];
  introduceAim: string;
  introduceRide: string;
  introduceData: string;
  introduceWear: string;
  introduceFee: string;
}

export const model = {
  type: Array,
  startEndCity: mongoose.Schema.Types.Mixed,
  aimPlace: mongoose.Schema.Types.Mixed,
  meetPlace: mongoose.Schema.Types.Mixed,
  spendTime: String,
  imgs: mongoose.Schema.Types.Mixed,
  durationTeam: String,
  phone: String,
  clubName: String,
  enrollTpl: Array,
  introduceAim: String,
  introduceRide: String,
  introduceData: String,
  introduceWear: String,
  introduceFee: String,
};

export const Schema = new mongoose.Schema(model);

const m: any = mongoose.model<IGood>("Good", Schema);

m.findByEmail = async function(email: string) {
  const res = await m.findOne({ email });
  return res;
};

m.findByPhone = async function(phone: string) {
  const res = await m.findOne({ phone });
  return res;
};

m.hasEmail = async function(email: string) {
  const res = await m.findByEmail(email);
  let ret = true;
  if (!res) {
    ret = false;
  }
  return ret;
};

m.hasPhone = async function(phone: string) {
  const res = await m.findByEmail(phone);
  let ret = true;
  if (!res) {
    ret = false;
  }
  return ret;
};

m.validatePassword = async function(username: string, password: string, type: number) {
  let ret = false;

  if (type === isEmail) {
    const res = await m.findByEmail(username);

    ret = res.password == password;
  }
  if (type === isPhone) {
    const res = await m.findByPhone(username);
    ret = res.password == password;
  }
  return ret;
};

export default m;
